#ifndef EM80_GP_INPUT_HPP
#define EM80_GP_INPUT_HPP

#include "../packet_base.hpp"
#include "../payload_headers.hpp"
#include "../em80_payload_factory.h"
#include "../../v8stdint.h"

#include <memory>

namespace em80_driver
{

class GPInput : public EM80PayloadBase
{
public:
    GPInput(const EM80PacketPtr &packet) : EM80PayloadBase(packet, "GPInput", false, 2) {}

    struct Data
    {
        uint8_t button;
        uint8_t gp_input;
    };

    struct Flag
    {
        static const uint8_t BUTTON_EMERGENCY = 0x01;
        static const uint8_t BUTTON_RESET = 0x02;
        static const uint8_t BUTTON_START = 0x04;
    };

    Data data;

    ~GPInput() {}

    //not to be used directly by external source
    bool serialize()
    {
        // //TODO : is it directly control the raw data
        // std::shared_ptr<Buffer> byteStream = std::make_shared<Buffer>(packet->payload(), packet->packet().second);
        // buildBytes(static_cast<uint8_t>(Header::GPInput), *byteStream);
        // buildBytes(static_cast<uint8_t>(length), *byteStream);

        // buildBytes(data.button, *byteStream);
        // buildBytes(data.gp_input, *byteStream);

        return true;
    }


    bool deserialize()
    {
        std::vector<uint8_t> byteStream;

        byteStream.assign(packet->payload(), packet->payload() + static_cast<uint32_t>(length) + 2);


        //TODO: check them
        uint8_t header_id, length_packed;
        buildVariable(header_id, byteStream);
        buildVariable(length_packed, byteStream);
        if(header_id != Header::GPInput) return false;
        if(length_packed != length) return false;

        buildVariable(data.button, byteStream);
        buildVariable(data.gp_input, byteStream);

        return true;
    }
};


} // namespace em80_driver

#endif /* EM80_FIRMWARE_HPP*/
